<?php

namespace app\util;
class AutoUtil
{
    /**
     * Desc: 生成controller
     * Datetime: 2024/1/3 15:40 by zdb
     */
    public static function makeControllerFile($controllerFolder, $serviceFolder, $subject, $title)
    {
        $projectPath = CommonUtil::getProjectPath();
        $controllerTplPath = $projectPath.'/example/controller/Example.php';
        $content = file_get_contents($controllerTplPath);

        $upperCamel = CommonUtil::covertUnderlineToUpperCamel($subject);
        $lowerCamel = CommonUtil::covertUnderlineToLowerCamel($subject);
        $content = str_replace('Example', $upperCamel, $content);

        $content = str_replace('Example', $upperCamel, $content);
        $content = str_replace('##package##', strtolower($controllerFolder), $content);
        $content = str_replace('##service_package##', strtolower($serviceFolder), $content);
        $content = str_replace('##title##', $title, $content);

        $dirPath = $projectPath.'/app/api/controller/'.$controllerFolder.'/';
        if(!is_dir($dirPath)){
            mkdir($dirPath);
        }
        $target = $dirPath.'/'.$upperCamel.".php";
        touch($target);
        file_put_contents($target, $content);

        //admin页面部分
        $controllerTplPath = $projectPath.'/example/controller/ExamplePage.php';
        $content = file_get_contents($controllerTplPath);

        $content = str_replace(['ExamplePage', 'Example', '##package##', '##DIR##'], [$upperCamel, $upperCamel, $controllerFolder, $subject], $content);

        $dirPath = $projectPath.'/app/admin/controller/'.$controllerFolder.'/';
        if(!is_dir($dirPath)){
            mkdir($dirPath);
        }
        $target = $dirPath.'/'.$upperCamel.".php";
        touch($target);
        file_put_contents($target, $content);
    }

    /**
     * Desc: 生成service文件
     * Datetime: 2024/1/3 15:41 by zdb
     */
    public static function makeServiceFile($controllerDir, $subject, $tableName)
    {
        $projectPath = CommonUtil::getProjectPath();
        $serviceTplPath = $projectPath.'/example/service/ExampleService.php';
        $content = file_get_contents($serviceTplPath);

        $upperCamel = CommonUtil::covertUnderlineToUpperCamel($subject);
        $content = str_replace('Example', $upperCamel, $content);
        $content = str_replace('##table_name##', strtolower($tableName), $content);
        $content = str_replace('##package##', strtolower($controllerDir), $content);

        $dirPath = $projectPath.'/app/api/service/'.$controllerDir.'/';
        if(!is_dir($dirPath)){
            mkdir($dirPath);
        }
        $target = $dirPath.'/'.$upperCamel."Service.php";
        touch($target);
        file_put_contents($target, $content);
    }

    public static function makeModelFile($package, $subject, $tableName)
    {
        $projectPath = CommonUtil::getProjectPath();
        $serviceTplPath = $projectPath.'/example/model/ExampleModel.php';
        $content = file_get_contents($serviceTplPath);

        $upperCamel = CommonUtil::covertUnderlineToUpperCamel($subject);
        $content = str_replace('Example', $upperCamel, $content);
        $content = str_replace('##table_name##', strtolower($tableName), $content);
        $content = str_replace('##package##', strtolower($package), $content);

        $dirPath = $projectPath.'/app/api/model/'.$package.'/';
        if(!is_dir($dirPath)){
            mkdir($dirPath);
        }
        $target = $dirPath.'/'.$upperCamel."Model.php";
        touch($target);
        file_put_contents($target, $content);
    }


    public static function makeViewFileOld($package, $subject, $columnInfo, $titleCn)
    {
        $html = '';
        foreach($columnInfo as $item){
            $fieldName = $item['field_name'];
            $html .= '<dl class="row">
                        <dt class="tit">
                            <label for="'.$fieldName.'"><em>*</em>'.$item['field_title'].'</label>
                        </dt>
                        <dd class="opt">';
            switch($item['field_type']){
                case 'datetime':
                case 'text':
                    $html .= '<input type="text" value="" name="'.$fieldName.'" id="'.$fieldName.'" class="input-txt" maxlength="50" />';
                    break;
                case 'select':
                    $html .= '< name="'.$fieldName.'" class="input-txt" />';
                case 'textarea':
                    $html .= '<textarea id="'.$fieldName.'" name="'.$fieldName.'" class="input-textarea" maxlength=255 ></textarea>';
                    break;
                case 'file':
                    $buttonId = "btn_upload_".$subject."_cover";
                    $html .= '<input type="file" name="image" id="'.$buttonId.'" >';
                    break;
            }
            $html .= '                <span class="err"></span>
                            <p class="notic"></p>
                        </dd>
                    </dl>';
        }

        $projectPath = CommonUtil::getProjectPath();
        $viewTplPath1 = $projectPath.'/example/view/list.php';
        $content = file_get_contents($viewTplPath1);

        $content = str_replace(['##subject##', '##subject_cn##'], [$subject, $titleCn], $content);

        $upperCamel = CommonUtil::covertUnderlineToUpperCamel($subject);
        $dirPath = $projectPath.'/admin/view/'.$package.'/'.$subject;
        if(!is_dir($dirPath)){
            mkdir($dirPath, '0777');
        }
        $target = $dirPath.'/list'.$upperCamel.'.php';
        touch($target);
        file_put_contents($target, $content);

        $viewTplPath2 = $projectPath.'/example/view/add.php';
        $content = file_get_contents($viewTplPath2);
        $content = str_replace(['##subject##'], [$subject], $content);
        $content = str_replace(['###REPLACE_CONTENT###'], [$html], $content);

        $target = $dirPath.'/add'.$upperCamel.'.php';
        touch($target);
        file_put_contents($target, $content);

        $viewTplPath3 = $projectPath.'/example/view/edit.php';
        $content = file_get_contents($viewTplPath3);
        $content = str_replace(['##subject##'], [$subject], $content);
        $content = str_replace(['###REPLACE_CONTENT###'], [$html], $content);

        $target = $dirPath.'/edit'.$upperCamel.'.php';
        touch($target);
        file_put_contents($target, $content);
    }

    public static function makeViewFile($subject, $pageSubject)
    {
        $projectPath = CommonUtil::getProjectPath();
        $viewTplPath1 = $projectPath.'/example/view/listExample.html';
        $content = file_get_contents($viewTplPath1);

        $content = str_replace(['##example##'], [$subject], $content);

        $upperCamel = CommonUtil::covertUnderlineToUpperCamel($subject);
        $dirPath = $projectPath.'/app/admin/view/'.$pageSubject;
        if(!is_dir($dirPath)){
            mkdir($dirPath, '0777', true);
        }
        $target = $dirPath.'/list'.$upperCamel.'.html';
        touch($target);
        file_put_contents($target, $content);

        $viewTplPath2 = $projectPath.'/example/view/addExample.html';
        $content = file_get_contents($viewTplPath2);
        $content = str_replace(['##example##'], [$subject], $content);

        $target = $dirPath.'/add'.$upperCamel.'.html';
        touch($target);
        file_put_contents($target, $content);

//        $viewTplPath3 = $projectPath.'/example/view/edit.php';
//        $content = file_get_contents($viewTplPath3);
//        $content = str_replace(['##subject##'], [$subject], $content);
//        $content = str_replace(['###REPLACE_CONTENT###'], [$html], $content);
//
//        $target = $dirPath.'/edit'.$upperCamel.'.php';
//        touch($target);
//        file_put_contents($target, $content);
    }

}
